package cn.fl.risk.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.condition.SysOrgQueryCondition;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.vo.OrgTreeVO;
import cn.fl.risk.condition.RskUsrOrgRQueryCondition;
import cn.fl.risk.dto.RskUsrOrgRDTO;
import cn.fl.risk.service.IRskUsrOrgRApiService;
import cn.fl.risk.service.IRskUsrOrgRService;
import cn.fl.risk.transform.RskUsrOrgRVOTransform;
import cn.fl.risk.vo.RskUsrOrgRVO;

/**
 * @Description: 租金提醒用户与组织架构关系表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class RskUsrOrgRServiceImpl implements IRskUsrOrgRService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(RskUsrOrgRServiceImpl.class);
	
	/**
	 * 租金提醒用户与组织架构关系表 暴露服务
	 */
	@Autowired
	private IRskUsrOrgRApiService rskUsrOrgRApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    /**
     * 组织架构服务
     */
    @Autowired
    private ISysOrgApiService sysOrgApiService;
	@Override
	public RskUsrOrgRVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		RskUsrOrgRDTO dto = this.rskUsrOrgRApiService.selectRskUsrOrgRById(id);
		return RskUsrOrgRVOTransform.toVO(dto);
	}

	@Override
	public Long add(RskUsrOrgRVO entity) throws BaseException{
		if(null == entity) return null;
		return this.rskUsrOrgRApiService.addRskUsrOrgR(RskUsrOrgRVOTransform.toDTO(entity));
	}
    
	@Override
	public boolean addOrgr(List<RskUsrOrgRVO> list) throws BaseException{
		if(null == list) return false;
		return this.rskUsrOrgRApiService.addRskUsrOrgRList(RskUsrOrgRVOTransform.toDTOList(list));
	}
	
	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.rskUsrOrgRApiService.removeRskUsrOrgRByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(RskUsrOrgRVO entity) throws BaseException{
		if(null == entity) return false;
		return this.rskUsrOrgRApiService.modifyRskUsrOrgR(RskUsrOrgRVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        RskUsrOrgRQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new RskUsrOrgRQueryCondition());
        }
		condition=(RskUsrOrgRQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.rskUsrOrgRApiService.searchRskUsrOrgRListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<RskUsrOrgRVO> voList=RskUsrOrgRVOTransform.toVOList((List<RskUsrOrgRDTO>)page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<RskUsrOrgRVO> searchList(RskUsrOrgRQueryCondition condition) throws BaseException{
		if(null == condition) condition=new RskUsrOrgRQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<RskUsrOrgRVO> voList=RskUsrOrgRVOTransform.toVOList(this.rskUsrOrgRApiService.searchRskUsrOrgRList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<RskUsrOrgRVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		/*Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, RskUsrOrgRConstant.CODE_CD);
		for(RskUsrOrgRVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getCd())) {
            	vo.setCdNm(_CdMap.get(vo.getCd()));
        	}	
		}*/
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }
	
    /**
     * 根据名称查询组织结构树
     * @param orgNm
     * @return List<OrgTreeVO>
     */
    @Override
    public List<OrgTreeVO> serchOrgTreeList(String orgNm){
    	List<SysOrgDTO> orgList = null;
    	SysOrgQueryCondition qc=new SysOrgQueryCondition();
    	if(StringUtils.isNotEmpty(orgNm)){
    		qc.setOrgNm(orgNm);
    		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
    		orgList=this.sysOrgApiService.searchSysOrgList(qc);
    	}
    	List<OrgTreeVO> list = null;
        if(CollectionUtils.isNotEmpty(orgList)){
            list = new ArrayList<>(orgList.size());
            OrgTreeVO orgTreeVO;
            for (SysOrgDTO org : orgList) {
                orgTreeVO=new OrgTreeVO();
                orgTreeVO.setOrgCd(org.getOrgCd());
                orgTreeVO.setId(org.getId());
                orgTreeVO.setOrgNm(org.getOrgDispNm());
                orgTreeVO.setPid(org.getOrgPid());
                //orgTreeVO.setChildren(serchOrgTreeListById(org.getId()));
                list.add(orgTreeVO);
            }
        }
        return list;
    }
    /**
     * 根据名称查询组织结构树
     * @param orgId
     * @return
     */
    @Override
    public List<OrgTreeVO> serchOrgTreeListById(Long orgId){
    	List<SysOrgDTO> orgList = null;
        if (StringUtils.isNotEmpty(orgId)) {
            orgList = this.sysOrgApiService.searchSysOrgByOrgPid(orgId, CurrentThreadContext.getCurrentSysCd());
        }
        List<OrgTreeVO> list = null;
        if(CollectionUtils.isNotEmpty(orgList)){
            list = new ArrayList<>(orgList.size());
            OrgTreeVO orgTreeVO;
            for (SysOrgDTO org : orgList) {
                orgTreeVO=new OrgTreeVO();
                orgTreeVO.setOrgCd(org.getOrgCd());
                orgTreeVO.setId(org.getId());
                orgTreeVO.setOrgNm(org.getOrgDispNm());
                orgTreeVO.setPid(org.getOrgPid());
                orgTreeVO.setChildren(serchOrgTreeListById(org.getId()));
                list.add(orgTreeVO);
            	}
        }
        return list;
    }
}
